package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.domain.ClickStrategyDetail;
import com.lebang.model.Whether;
import com.lebang.model.enums.buss.RewardTypeUscan;
import com.lebang.service.ClickStrategyDetailService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;


/**
 * 签到策略配置
 *
 * @author hwq
 * @date 2020-02-25
 */
@Controller
@RequestMapping("/sys/clickstrategydetail")
public class ClickStrategyDetailController {
    @Autowired
    private ClickStrategyDetailService baseService;
    @Login
    @GetMapping("/list")
    public String list(Model model,Integer strategyId) {
        model.addAttribute("strategyId",strategyId);
        return "/clickstrategydetail/list";
    }
    @Login
    @GetMapping("/add")
    public String add(Long id, Model model,Integer strategyId) {
        ClickStrategyDetail vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new ClickStrategyDetail();
            vo.setStrategyId(strategyId);
            vo.setRewardType(RewardTypeUscan.Point.getIndex());
            vo.setHasSendMsg(Whether.NO.getDbIndex());
        }
        model.addAttribute("item", vo);
        return "/clickstrategydetail/edit";
    }

    @Login
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(ClickStrategyDetail vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(Whether.NO.getDbIndex());
            baseService.insertModel(vo);
        }

        return Callback.success("success", "修改成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Long id) {
        baseService.deleteById(id);
        return Callback.success("success", "删除成功");
    }

}
